//读取结构体

#include <iostream>
#include <fstream>
using namespace std;
struct Student
{
    char name[100];
    int num;
    int age;
    char sex;
};
int main()
{
        // Student s[3] = {
        // {"li",1001,18,'f'},
        // {"Fun",1002,19,'m'},
        // {"Wang",1004,17,'f'}
        // };
        ofstream
        ofs("student.txt", ios::out | ios::trunc | ios::binary);
    // if(!ofs){
    // cout<<"open error"<<endl;
    // }
    // for(int i=0; i<3; i++)
    // {
    // ofs.write((char*)&s[i],sizeof(s[i]));
    // }
    // ofs.close();
    Student s;
    ifstream ifs("student.txt", ios::in | ios::binary);
    if (!ifs)
        cout << "open error" << endl;
    ifs.seekg(sizeof(s), ios::beg);
    while (ifs.read((char *)&s, sizeof(Student)), !ifs.eof())
    {
        cout << "Name " << s.name << endl;
        cout << "Num " << s.num << endl;
        cout << "Age " << s.age << endl;
        cout << "Sex " << s.sex << endl;
        cout << "---------------" << endl;
    }
    ifs.close();
    return 0;
}